package com.example.test.Utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtils<T> {
    private static DruidDataSource dataSource;
    private Class<T> clazz=null;
    static {
        try {
            Properties properties = new Properties();
            InputStream rs = ClassLoader.getSystemResourceAsStream("druid.properties");
            properties.load(rs);
            dataSource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //1.获取数据库连接
    public static Connection getCon() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //2.获取数据库连接
    public static void close(Connection con) {
        try {
            DbUtils.close(con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
